from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from options import options



class Discuz():

    def __init__(self,driver):
        self.driver=driver
    def login(self,username,password):
        # 登录
        self.driver.find_element(By.ID, 'ls_username').clear()
        self.driver.find_element(By.ID, 'ls_username').send_keys(username)
        self.driver.find_element(By.ID, 'ls_password').clear()
        self.driver.find_element(By.ID, 'ls_password').send_keys(password)
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em').click()
        sleep(3)

    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)
    def read(self):
        # 进入默认板块
        self.driver.find_element(By.LINK_TEXT, '默认版块').click()
        sleep(6)
        # 查看帖子
        self.driver.find_element(By.XPATH, '//*[@id="normalthread_39"]/tr/th/a[2]').click()
        sleep(2)
    def register(self,username,password,email):
        # 注册
        self.driver.find_element(By.LINK_TEXT, '立即注册').click()
        sleep(3)
        # 注册用户名
        self.driver.find_element(By.XPATH, '//*[@id="Zn2io6"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="Zn2io6"]').send_keys(username)
        sleep(2)
        # 输入密码
        self.driver.find_element(By.XPATH, '//*[@id="ckRN4W"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="ckRN4W"]').send_keys(password)
        sleep(2)
        # 确认密码
        self.driver.find_element(By.XPATH, '//*[@id="jKvX92"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="jKvX92"]').send_keys(password)
        sleep(2)
        # 输入email
        self.driver.find_element(By.XPATH, '//*[@id="s6uuMS"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="s6uuMS"]').send_keys(email)
        # 提交
        self.driver.find_element(By.XPATH, '//*[@id="registerformsubmit"]').click()
        sleep(4)

    def post(self,title,text):
        # 发帖

        # 进入默认板块
        self.driver.find_element(By.LINK_TEXT, '默认版块').click()
        sleep(3)
        # 发帖
        self.driver.find_element(By.ID, 'newspecial').click()
        sleep(1)
        # 写入题目
        self.driver.find_element(By.ID, 'subject').clear()
        self.driver.find_element(By.ID, 'subject').send_keys(title)
        sleep(2)
        # 写入内容
        # 注意：嵌套页面的处理
        # 跳转到碎片页面
        self.driver.switch_to.frame('e_iframe')
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys(text)
        sleep(2)
        # 跳回默认页面
        self.driver.switch_to.default_content()
        self.driver.find_element(By.XPATH, '//*[@id="postsubmit"]').click()
        sleep(3)

    def reply(self,reply):
        # 进入默认板块
        self.driver.find_element(By.LINK_TEXT, '默认版块').click()
        sleep(3)
        # 查看帖子
        self.driver.find_element(By.XPATH, '//*[@id="normalthread_38"]/tr/th/a[2]').click()
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="post_reply"]').click()
        sleep(2)
        # 输入回复内容
        self.driver.find_element(By.ID, 'postmessage').send_keys(reply)
        sleep(2)
        # 点击回复按钮
        self.driver.find_element(By.XPATH, '//*[@id="postsubmit"]').click()
        sleep(2)


if __name__ == '__main__':

    url = r'http://192.168.126.131/upload/'
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    driver.maximize_window()
    sleep(1)

    discuz=Discuz(driver)

    username='admin'
    password='123456'
    #测试登录
    discuz.login(username,password)
    #测试退出
    discuz.logout()

    driver.quit()

